<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<div id="page-content">

    <c:forEach var="module" items="${moduleNameList}" varStatus="status">
        <div class="row" style="margin-bottom:20px;">
            <div class="col-lg-12 col-sm-12 col-xs-12" id="${status.index}_div">
                <div class="panel">
                    <h3 class="panel_title">${module}</h3>
                    <div class="col-sm-12 table-toolbar-right" style="position: absolute; right: 15px;top: 5px;">
                        <a id="${status.index}_more" class= btn-default" style="cursor:pointer;" onclick="chooseForm('${module}')">
                            <img src="../resource/core/img/index/u56.png" width="50px"/>
                        </a>
                    </div>
                    <div id="${module}_list"></div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
    </c:forEach>

    <c:if test="${fn:length(moduleNameList)<=0}">
        <div class="panel" style="height: 600px">
            <div class="col-lg-6 col-lg-offset-3">
                <div class="panel panel-trans text-center">
                    <h3 class="panel-title" style="margin-top: 250px;color: #9da9b6">您目前没有任何权限</h3>
                </div>
            </div>
        </div>
    </c:if>

</div>
<script>

    $(function(){
        __ajax_get("<%=path%>/com/startExam/getStartExamList",null,function(data){
            for(var i=0;i<data.length;i++){
                var newItem='<div class="panel_left" onclick="toInput(\''+data[i].formKey+'\')">'
                        +'<img src="../resource/core/img/index/u75.png" width="70px"/>'
                        +'<p>'+data[i].formName+'<p>'
                        +'<input type="hidden" name="formKey" value="'+data[i].formKey+'" />'
                        +'</div>';
                $("#"+data[i].moduleName+"_list").append(newItem);
            }
        })
    });

    function chooseForm(moduleName){
        var thisListDiv=$("#"+moduleName+"_list");
        var cKeys="";
        thisListDiv.find(":input[name='formKey']").each(function(){
            cKeys+=$(this).val()+",";
        });
        cKeys = cKeys.substr(0,cKeys.length-1);

        __open_dialog_select("请选择"+moduleName+"下的表单","<%=path%>/com/startExam/chooseForm?moduleName="+moduleName+"&cKeys="+cKeys,function(dialogRef){
            var callback = function (data) {
                if (data) {
                    var nameArr = data.names.split(",");
                    var keyArr = data.ids.split(",");

                    var url="<%=path%>/com/startExam/startExamEdit";
                    var param={'moduleName':moduleName,'formName':data.names,'formKey':data.ids};
                    __ajax_post(url,param,function(pdata){
                        if(pdata.state==1){
                            thisListDiv.empty();
                            for(var i=0;i<nameArr.length;i++){
                                if(nameArr[i]!=""){
                                    var newItem='<div class="panel_left" onclick="toInput(\''+keyArr[i]+'\')">'
                                            +'<img src="../resource/core/img/index/u75.png" width="70px"/>'
                                            +'<p>'+nameArr[i]+'<p>'
                                            +'<input type="hidden" name="formKey" value="'+keyArr[i]+'" />'
                                            +'</div>';
                                    thisListDiv.append(newItem);
                                }
                            }
                            __toastr_success(pdata.message);
                            dialogRef.close();
                        }else{
                            __toastr_error("保存失败");
                            dialogRef.enableButtons();
                        }
                    });

                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_forms_select(callback);
        });
    }

    function toInput(formKey){
        var entityName=formKey.substring(formKey.lastIndexOf(".")+1,formKey.length);
        var a=entityName.charAt(0).toLowerCase();
        entityName=a+entityName.substring(1,entityName.length);
        loadURL("<%=path%>/com/"+entityName+"/input", $('#page-content'));
    }


</script>
